﻿using MCUPic.Core.Pic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace MCUPic.Core
{
    /// <summary>
    /// 按照配置处理任意格式图片
    /// </summary>
    public class PicConverter
    {
        Graphics Graphics;
        public Bitmap SrcImage;
        public PicConverter()
        {
        }

        private delegate Size GetScaleSize(Size PicSize, Size ScreenSize);
        static GetScaleSize[] getScaleSizes = new GetScaleSize[]
        {
            null,
            null,
            null,
            Fit_Size,
            FullFill_Size
        };

        private static Size Fit_Size(Size PicSize, Size ScreenSize)
        {
            Size size;
            // 缩放因子 p
            double p = 0;
            if (PicSize.Width * ScreenSize.Height > PicSize.Height * ScreenSize.Width)
            {
                p = (double)ScreenSize.Width / PicSize.Width;
                size = new Size(ScreenSize.Width, (int)(PicSize.Height * p));
            }
            else
            {
                p = (double)ScreenSize.Height / PicSize.Height;
                size = new Size((int)(PicSize.Width * p), ScreenSize.Height);
            }

            return size;
        }

        private static Size FullFill_Size(Size PicSize, Size ScreenSize)
        {
            Size size;
            // 缩放因子 p
            double p = 0;
            if (PicSize.Width * ScreenSize.Height < PicSize.Height * ScreenSize.Width)
            {
                p = (double)ScreenSize.Width / PicSize.Width;
                size = new Size(ScreenSize.Width, (int)(PicSize.Height * p));
            }
            else
            {
                p = (double)ScreenSize.Height / PicSize.Height;
                size = new Size((int)(PicSize.Width * p), ScreenSize.Height);
            }

            return size;
        }

        internal Bitmap DrawBmp(Bitmap srcBmp, Rectangle rectangle, PixelFormat pixelFormat)
        {
            Bitmap DstBmp = new Bitmap(rectangle.Size.Width, rectangle.Size.Height, pixelFormat);
            Graphics = Graphics.FromImage(DstBmp);
            Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Graphics.DrawImageUnscaled(srcBmp, rectangle);
            Graphics.Dispose();
            return DstBmp;
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public Bitmap Scale(Bitmap srcBmp, Size ScreenSize, ScaleMode scaleMode)
        {
            // 等比例缩放
            Size tmpSize = TrimZoom(srcBmp.Size, ScreenSize, scaleMode);
            return new Bitmap(srcBmp, tmpSize);
        }

        private Size TrimZoom(Size bmpSize, Size ScreenSize, ScaleMode scaleMode)
        {
            return getScaleSizes[(int)scaleMode](bmpSize, ScreenSize);
        }
    }

    public enum ScaleMode
    {
        /* UnScale */
        Center,
        FullTile,
        /* Scale */
        Stretch,
        Fit,
        FullFill
    }
}
